﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;

namespace DiamondDash.Core
{
    public class StageManager
    {
        public static StageManager Instance = new StageManager();

        //keys
        private const string IS_PLAY_BACKGROUND_KEY = "isPlayBackgroundKey";
        private const string IS_PLAY_SOUND_KEY = "isPlaySoundKey";
        private const string TOTALGKEY = "TotalGKey";
        private const string CURR_LEVEL_KEY = "CURR_LEVEL_KEY";
        private const string CURR_LEVEL_EXP_KEY = "CURR_LEVEL_EXP_KEY";
        private const string HIGH_SCORE_KEY = "HIGH_SCORE_KEY";
        private const string BOMB_COUNT_KEY = "BOMB_COUNT_KEY";
        private const string TIME_COUNT_KEY = "TIME_COUNT_KEY";
        private const string COLOR_COUNT_KEY = "COLOR_COUNT_KEY";

        private const string LAUNCH_COUNT = "launchCount";
        private const string MARKET_COMMENT = "marketComment";
        private const string NEW_USER_TASK_KEY = "newUserTaskKey";

        private const string IS_FIRET_UNLOCKTOOL_KEY = "IS_FIRET_UNLOCKTOOL_KEY";

        private const string LAST_FULLSCREEN_DATE_KEY = "LAST_FULLSCREEN_DATE";

        public bool TurnMoka = true;

        private StageManager()
        {
            //初始化

        }

        /// <summary>
        /// 初始化工作
        /// </summary>
        public void Init()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAUNCH_COUNT))
            {
                launchCount = (int)IsolatedStorageSettings.ApplicationSettings[LAUNCH_COUNT];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(MARKET_COMMENT))
            {
                isCommented = (bool)IsolatedStorageSettings.ApplicationSettings[MARKET_COMMENT];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(NEW_USER_TASK_KEY))
            {
                isDoNewUserTask = (bool)IsolatedStorageSettings.ApplicationSettings[NEW_USER_TASK_KEY];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(TOTALGKEY))
            {
                totalG = (int)IsolatedStorageSettings.ApplicationSettings[TOTALGKEY];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(CURR_LEVEL_KEY))
            {
                currLevel = (int)IsolatedStorageSettings.ApplicationSettings[CURR_LEVEL_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(CURR_LEVEL_EXP_KEY))
            {
                currLevelExp = (int)IsolatedStorageSettings.ApplicationSettings[CURR_LEVEL_EXP_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(HIGH_SCORE_KEY))
            {
                highScore = (int)IsolatedStorageSettings.ApplicationSettings[HIGH_SCORE_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(BOMB_COUNT_KEY))
            {
                bombCount = (int)IsolatedStorageSettings.ApplicationSettings[BOMB_COUNT_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(TIME_COUNT_KEY))
            {
                timeCount = (int)IsolatedStorageSettings.ApplicationSettings[TIME_COUNT_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(COLOR_COUNT_KEY))
            {
                colorCount = (int)IsolatedStorageSettings.ApplicationSettings[COLOR_COUNT_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(IS_PLAY_SOUND_KEY))
            {
                isPlaySound = (bool)IsolatedStorageSettings.ApplicationSettings[IS_PLAY_SOUND_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAST_FULLSCREEN_DATE_KEY))
            {
                lastFullScreenDate = (string)IsolatedStorageSettings.ApplicationSettings[LAST_FULLSCREEN_DATE_KEY];
            }
            //if (IsolatedStorageSettings.ApplicationSettings.Contains(IS_PLAY_BACKGROUND_KEY))
            //{
            //    isPlayBackground = (bool)IsolatedStorageSettings.ApplicationSettings[IS_PLAY_BACKGROUND_KEY];
            //}

            if (IsolatedStorageSettings.ApplicationSettings.Contains(IS_FIRET_UNLOCKTOOL_KEY + 1))
            {
                isFirstUnlockBomb = (bool)IsolatedStorageSettings.ApplicationSettings[IS_FIRET_UNLOCKTOOL_KEY + 1];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(IS_FIRET_UNLOCKTOOL_KEY + 2))
            {
                isFirstUnlockTime = (bool)IsolatedStorageSettings.ApplicationSettings[IS_FIRET_UNLOCKTOOL_KEY + 2];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(IS_FIRET_UNLOCKTOOL_KEY + 3))
            {
                isFirstUnlockColor = (bool)IsolatedStorageSettings.ApplicationSettings[IS_FIRET_UNLOCKTOOL_KEY + 3];
            }
        }

        int ExpBase = 2000;
        int LowLevelExpBase = 500;

        public double GetLevelExpPercent()
        {
            return (CurrLevelExp / (double)(CurrLevel == 1 ? LowLevelExpBase : ExpBase)) * 100;
        }

        public bool AddExp(int exp)
        {
            bool result = false;
            if (CurrLevel >= 99)
            {
                CurrLevelExp = 0;
            }
            else
            {
                CurrLevelExp += exp;
                if (CurrLevelExp >= (CurrLevel == 1 ? LowLevelExpBase : ExpBase))
                {
                    CurrLevelExp = CurrLevelExp % (CurrLevel == 1 ? LowLevelExpBase : ExpBase);
                    CurrLevel++;
                    if (CurrLevel >= 99)
                    {
                        CurrLevelExp = 0;
                    }
                    else
                    {
                        result = true;
                    }
                }
            }
            return result;
        }


        #region 运行时变量

        /// <summary>
        /// 最近一次游戏的分数
        /// </summary>
        public int LastestGameScore { get; set; }

        #endregion

        #region 存储的变量

        private string lastFullScreenDate = "XXXXXX";
        /// <summary>
        /// 上一次显示全屏广告的日期
        /// </summary>
        public string LastFullScreenDate
        {
            get
            {
                return lastFullScreenDate;
            }
            set
            {
                lastFullScreenDate = value;
                IsolatedStorageSettings.ApplicationSettings[LAST_FULLSCREEN_DATE_KEY] = lastFullScreenDate;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isFirstUnlockBomb = true;
        /// <summary>
        /// 第一次解锁炸弹
        /// </summary>
        public bool IsFirstUnlockBomb
        {
            get
            {
                return isFirstUnlockBomb;
            }
            set
            {
                isFirstUnlockBomb = value;
                IsolatedStorageSettings.ApplicationSettings[IS_FIRET_UNLOCKTOOL_KEY + 1] = isFirstUnlockBomb;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isFirstUnlockTime = true;
        /// <summary>
        /// 第一次解锁时间道具
        /// </summary>
        public bool IsFirstUnlockTime
        {
            get
            {
                return isFirstUnlockTime;
            }
            set
            {
                isFirstUnlockTime = value;
                IsolatedStorageSettings.ApplicationSettings[IS_FIRET_UNLOCKTOOL_KEY + 2] = isFirstUnlockTime;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isFirstUnlockColor = true;
        /// <summary>
        /// 第一次解锁颜色道具
        /// </summary>
        public bool IsFirstUnlockColor
        {
            get
            {
                return isFirstUnlockColor;
            }
            set
            {
                isFirstUnlockColor = value;
                IsolatedStorageSettings.ApplicationSettings[IS_FIRET_UNLOCKTOOL_KEY + 3] = isFirstUnlockColor;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }


        private bool isPlaySound = true;

        /// <summary>
        /// 是否播放音效
        /// </summary>
        public bool IsPlaySound
        {
            get
            {
                return isPlaySound;
            }
            set
            {
                isPlaySound = value;
                IsolatedStorageSettings.ApplicationSettings[IS_PLAY_SOUND_KEY] = isPlaySound;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        //private bool isPlayBackground = true;
        ///// <summary>
        ///// 是否播放背景音乐
        ///// </summary>
        //public bool IsPlayBackground
        //{
        //    get
        //    {
        //        return isPlayBackground;
        //    }
        //    set
        //    {
        //        isPlayBackground = value;
        //        IsolatedStorageSettings.ApplicationSettings[IS_PLAY_BACKGROUND_KEY] = isPlayBackground;
        //        IsolatedStorageSettings.ApplicationSettings.Save();
        //    }
        //}

        private int launchCount = 1;
        public int LaunchCount
        {
            get
            {
                return launchCount;
            }
            set
            {
                launchCount = value;
                IsolatedStorageSettings.ApplicationSettings[LAUNCH_COUNT] = launchCount;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isDoNewUserTask = false;
        public bool IsDoNewUserTask
        {
            get
            {
                return isDoNewUserTask;
            }
            set
            {
                isDoNewUserTask = value;
                IsolatedStorageSettings.ApplicationSettings[NEW_USER_TASK_KEY] = isDoNewUserTask;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isCommented = false;
        public bool IsCommented
        {
            get
            {
                return isCommented;
            }
            set
            {
                isCommented = value;
                IsolatedStorageSettings.ApplicationSettings[MARKET_COMMENT] = isCommented;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int totalG = 0;
        /// <summary>
        /// 金币
        /// </summary>
        public int TotalG
        {
            get
            {
                return totalG;
            }
            set
            {
                totalG = value;
                IsolatedStorageSettings.ApplicationSettings[TOTALGKEY] = TotalG;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int highScore = 0;
        /// <summary>
        /// 最高分
        /// </summary>
        public int HighScore
        {
            get
            {
                return highScore;
            }
            set
            {
                highScore = value;
                IsolatedStorageSettings.ApplicationSettings[HIGH_SCORE_KEY] = highScore;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }


        private int currLevel = 1;
        /// <summary>
        /// 当前等级
        /// </summary>
        public int CurrLevel
        {
            get
            {
                return currLevel;
            }
            set
            {
                currLevel = value;
                IsolatedStorageSettings.ApplicationSettings[CURR_LEVEL_KEY] = currLevel;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int currLevelExp;
        /// <summary>
        /// 当前等级的经验值
        /// </summary>
        public int CurrLevelExp
        {
            get
            {
                return currLevelExp;
            }
            set
            {
                currLevelExp = value;
                IsolatedStorageSettings.ApplicationSettings[CURR_LEVEL_EXP_KEY] = currLevelExp;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int bombCount;
        public int BombCount
        {
            get
            {
                return bombCount;
            }
            set
            {
                bombCount = value;
                IsolatedStorageSettings.ApplicationSettings[BOMB_COUNT_KEY] = bombCount;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int timeCount;
        public int TimeCount
        {
            get
            {
                return timeCount;
            }
            set
            {
                timeCount = value;
                IsolatedStorageSettings.ApplicationSettings[TIME_COUNT_KEY] = timeCount;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int colorCount;
        public int ColorCount
        {
            get
            {
                return colorCount;
            }
            set
            {
                colorCount = value;
                IsolatedStorageSettings.ApplicationSettings[COLOR_COUNT_KEY] = colorCount;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        #endregion

    }
}
